package cn.wolfcode.education.web.controller;

import cn.wolfcode.education.anno.PermissionName;
import cn.wolfcode.education.domain.Role;
import cn.wolfcode.education.query.QueryObject;
import cn.wolfcode.education.service.IRoleService;
import cn.wolfcode.education.util.JSONResult;
import cn.wolfcode.education.util.PageResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("role")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @RequestMapping("")
    @PermissionName("角色页面管理")
    @RequiresPermissions("role:view")
    public String index(){
        return "role";
    }

    @RequestMapping("list")
    @ResponseBody
    @PermissionName("角色数据列表")
    @RequiresPermissions("role:query")
    public PageResult list(QueryObject qo){
        return roleService.listAll(qo);
    }

    @RequestMapping("listAll")
    @ResponseBody
    public List<Role> listAll(){
        return roleService.list();
    }

    @RequestMapping("save")
    @ResponseBody
    @PermissionName("角色添加")
    @RequiresPermissions("role:insert")
    public JSONResult save(Role role){
        JSONResult result = new JSONResult();
        try {
            roleService.save(role);
        }catch (Exception e){
            result.setMsg("保存失败");
        }
        return result;
    }

    @RequestMapping("edit")
    @ResponseBody
    @PermissionName("角色修改")
    @RequiresPermissions("role:update")
    public JSONResult update(Role role){
        JSONResult result = new JSONResult();
        try {
            roleService.update(role);
        }catch (Exception e){
            result.setMsg("更新失败");
        }
        return result;
    }

    @RequestMapping("listRoleByEmployeeId")
    @ResponseBody
    public List<Long> listRoleByEmployeeId(Long employeeId){
        return roleService.listRoleByEmployeeId(employeeId);
    }
}
